//
//  KINGWelcomeViewController.swift
//  肉荣微博
//
//  Created by king on 15/7/31.
//  Copyright © 2015年 king. All rights reserved.
//

import UIKit
import SDWebImage

class KINGWelcomeViewController: UIViewController {

    //MARK: - 约束
    private var iconBottomcons:NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    //动画一定要在这里进行显示，不然的会出现意想不到的效果
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
        if let url = KINGUserAccount.userAccount?.avatar_large {
            print(url)
            iconView.sd_setImageWithURL(NSURL(string: url))
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    /// 设置头像动画
    private func startAnimation() {
        iconBottomcons?.constant = UIScreen.mainScreen().bounds.height - iconBottomcons!.constant
        
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 4.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                //更新约束
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
                NSNotificationCenter.defaultCenter().postNotificationName(KINGRootControllerSwitchNSNotification, object: true)
        }
    }
    /// 布局UI
    private func setupUI() {
        view.addSubview(backgroundView)
        view.addSubview(iconView)
        view.addSubview(messageLabel)
        
        backgroundView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backgroud]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["backgroud":backgroundView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backgroud]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["backgroud":backgroundView]))
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 180))
        iconBottomcons = view.constraints.last
        
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    }
    //MARK: - 懒加载控件
    private lazy var backgroundView:UIImageView = {
        let background = UIImageView(image: UIImage(named: "ad_background"))
        return background
    }()
    /// 头像控件
    private lazy var iconView:UIImageView = {
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.cornerRadius = 45.0
        icon.layer.masksToBounds = true
        return icon
    }()
    /// 欢迎信息控件
    private lazy var messageLabel:UILabel = {
        let message = UILabel()
        message.text = "欢迎猪肉荣"
        message.textColor = UIColor.darkGrayColor()
        message.textAlignment = NSTextAlignment.Center
        message.sizeToFit()
        return message
    }()
}
